<?php
/**
 * 
 * 邀请用户
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}




define("CRON_NAME","cron_pubu_user_invite");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",100);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/album/inc.php");

ini_set("display_errors",ture);
error_reporting(E_ALL);

class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$mblog_from_api = new mblog_from_api();
		if(!$this->start_id)
		{
			$this->start_id=0;
		}
		$sql ="select count(*) as count from mblog_pic_content where id > {$this->start_id}";
		$count = $db_mblog_cron_topic_tag->select($sql);
		$count =  $count["count"];
		
		if($count == 0)
		{
			//如果获取不到需要更新的数据，初始化cron_start_id
			$this->ini_start_id(NOW_PROCESS);
			
		}
		
		
		for($i=1;$i<MAX_PAGE;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		
		
	}	
	
	function get_list_by_page($p)
	{
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		
		$start = ($p-1)*PCOUNT;
		$sql = "select topic_id,topic,id, mid,create_uid,show_type from mblog_pic_content where id > {$this->start_id} order by id  limit $start,".PCOUNT;
		
		$food_tag = $db_mblog_cron_topic_tag->select($sql);
		
		foreach($food_tag as $value)
		{
			//执行时间超过30分钟，退出
			if(time() - CRON_START_TIME > 3600)
			{
				exit;
			}
			if($value["show_type"] == 4 )
			{
				continue;
			}
			$mid = $value["mid"];
			$topic = $value["topic"];
			$create_uid = $value["create_uid"];
			$id = $value["id"];
			$topic_id = $value["topic_id"];
			if($mid)//处理抓取分页逻辑
			{
				$this->get_list_by_topic($create_uid,$mid,$topic,$id,$topic_id);
			}
	
			if($id)//更新抓取的start_id到数据库
			{
				$done_job = true;
				$res = $this->replace_into_data($id);	
			}
			
			
		}
		
			
	}
	
	
	//根据话题抓取数据并入库
	function get_list_by_topic($uid,$mid,$topic,$id,$topic_id="")
	{
		$db_base = new db_base();

		$ary["uid"] = $uid;
		$insert_res = $db_base->insert("user_pubu_invate",$ary);
		var_dump($insert_res);
		if(!$insert_res)
		{
			return false;
		}
		
		
		$sql ="select  uid,sex,verified_type from user_info where uid  = $uid limit 1";
		$res = $db_base->select($sql);
		if(!$res)
		{
			return false;
		}

		$sql = "select mid,id, topic from mblog_pic_content where create_uid = $uid order by create_time desc limit 1 ";
		$res2 = $db_base->select($sql);
		if($res2[0]["mid"])
		{
			$mid = $res2[0]["mid"];
			$topic = $res2[0]["topic"];
			$id = $res2[0]["id"];
		}
		$verified_type = $res[0]["verified_type"];
		$sex = $res[0]["sex"];
		
		
		$sql ="update user_pubu_invate set mid= $mid,create_time=now(), sex=$sex where uid = $uid";
		$db_base->query($sql);
	
		
		$db_album_info = new db_album_info();
		
		
		if($mid)
		{
			
			if($verified_type >0 && $verified_type < 8)
			{
				$content = "敬业的小编，由于您这条微博提到了{$topic},所以被收录到游戏秀了，有兴趣可以过来看看哦。。http://yxxiu.com/mblog/detail.php?id={$id} 如果您能把您意见反馈给我，万分感谢啊";
			}
			else
			{
				if($sex == 0)
				{
					$content = "尊敬的美女，由于您这条微博提到了{$topic},所以被收录到游戏秀了，有兴趣可以过来看看哦。。http://yxxiu.com/mblog/detail.php?id={$id} 如果您能把您意见反馈给我，万分感谢啊";
				}
				else
				{
					$content = "尊敬的帅哥，由于您这条微博提到了{$topic},所以被收录到游戏秀了，有兴趣可以过来看看哦。。http://yxxiu.com/mblog/detail.php?id={$id} 如果您能把您意见反馈给我，万分感谢啊";
				
				}
				
			}
			
			
			if($topic_id)
			{
				$album_info = $db_album_info->get_album_item_by_topic_id_is_tuijian($topic_id);		
				if($album_info)
				{
					$content.=" 如果您有四条以上提到{$topic}的，会自动为您创建专辑，比如-> http://yxxiu.com/album/detail.php?id={$album_info["id"]}";
				}
				else
				{
					$content.=" 如果您有四条以上提到{$topic}的，会自动为您创建专辑，比如-> http://yxxiu.com/album/?topic_id={$topic_id}.html";
				}
				
			}
			
			
			$admin_uid = 1737886622;
			$db_user_token = new db_user_token();
			$token_info  = $db_user_token->get_token_by_uid_and_appkey($admin_uid,WB_AKEY);
			$token = $token_info["access_token"];
			$SaeTClientV2 = new SaeTClientV2( WB_AKEY , WB_SKEY , $token);
			$res = $SaeTClientV2->send_comment( $mid, $content );
			
			$cid = $res["id"];
			if($cid)
			{
				$sql = "update user_pubu_invate set cid = $cid where uid = $uid";
				@$db_base->query($sql);
				sleep(5);
				
			}
			//调用太频繁就退出
			if($res["error_code"] == 10024 || $res["error_code"] == 20016  )
			{
				exit;
			}
		}
		
		
		
		
	}
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
